<?php
/**
* @software         [JiJinCMS]
* @author           [上海集锦信息科技有限公司]
* @website          [http://www.jijinweb.com]
* @email            [sunny@jijinweb.com]
**/
namespace app\index\model;
use app\common\page\Bootstrap;
use think\facade\Db;
use think\facade\Request;
//前台调用标签的模型函数
class LabelModel{
    public $config=[],//标签配置
            $where=[],//where条件
            $debugSql,//debug调试语句输出
            $table,//请表对象
            $tableName,//主表表名
            $tableNameData,//副表表名
            $data;//返回的数据
    public function __construct(array $config=array(),array $where=array()){
        //提取为空的where条件
        foreach($where as $k=>$v){
            if($v[2]===""){unset($where[$k]);}
        }
        //
        if($config){
            $this->config=$config;
        }
        if($where){
            $this->where=$where;
        }
    }
    //设置catid，针对list标签需要使用到
    public function setCatid(){
        //找出catid对应的where下标
        foreach($this->where as $key=>$v){
           if($v[0]=="catid"){
               $whereKey=$key;
           }
        }
        //如果是字符串的需要转为数组
        if(strstr($this->where[$whereKey][2],",")){
            $this->where[$whereKey][2]=explode(",",$this->where[$whereKey][2]);
        }
        //提取表名
        if(is_array($this->where[$whereKey][2])){
            $this->table=Db::name("cms_".$GLOBALS["cat"][$this->where[$whereKey][2][0]]['table_name']);
            $this->tableName="cms_".$GLOBALS["cat"][$this->where[$whereKey][2][0]]['table_name'];
            $this->tableNameData="cms_".$GLOBALS["cat"][$this->where[$whereKey][2][0]]['table_name']."_data";
        }else{
            $this->table=Db::name("cms_".$GLOBALS["cat"][$this->where[$whereKey][2]]['table_name']);
            $this->tableName="cms_".$GLOBALS["cat"][$this->where[$whereKey][2]]['table_name'];
            $this->tableNameData="cms_".$GLOBALS["cat"][$this->where[$whereKey][2]]['table_name']."_data";
        }
        //
        if(!$this->table){
            echo "栏目信息不存在";exit;
        }
        //查询子栏目
        if(isset($this->config['_son'])&&($this->config['_son']==0||$this->config['_son']==false)){
        }else{
            if(is_array($this->where[$whereKey][2])){
                $this->where[$whereKey]=["catid","IN", $this->where[$whereKey][2]];
            }else{
                $this->where[$whereKey]=["catid","IN", getSonCat($this->where[$whereKey][2])];
            }
        }
    }
    //设置查询条件
    public function setWhere(){
        if($this->where){
            $this->table->where($this->where);
        }
    }
    //设置表名，针对tabel标签用的
    public function setTable(){
        $this->table=Db::name($this->config["_table"]);
    }
    //查询指定字段
    public function setField(){
        $this->table->field($this->config['_field']);
    }
    //设置排序
    public function setOrder(){
        $this->table->order($this->config['_order']);
    }   
    //设置按ORDER BY field排序
    public function setOrderRaw(){
        $this->table->orderRaw($this->config['_orderRaw']);
    }       
    //having
    public function setHaving(){
        $this->table->having($this->config['_having']);
    }  
    //join
    public function setJoin(){
        $this->table->join($this->config['_join']);
    }     
    //按group排序
    public function setGroup(){
        $this->table->group($this->config['_group']);
    }      
    //限制条数
    public function setLimit(){
        //是不是int型
        if(is_numeric($this->config['_limit'])){
            $this->table->limit($this->config['_limit']);
        }else{
            $limitArr=explode(",",$this->config['_limit']);
            $this->table->limit($limitArr[0],$limitArr[1]);
        }
        
    }
    //设置分页
    public function setPaginate(){
        $this->config['_page_list_rows']=$this->config['_page_list_rows']?$this->config['_page_list_rows']:20;
        $this->config['_page_query']=$this->config['_page_query']?$this->config['_page_query']:Request::param();
        $this->config['_page_display_var']=$this->config['_page_display_var']?$this->config['_page_display_var']:"";
        $this->config['_page_url_var']=$this->config['_page_url_var']?$this->config['_page_url_var']:"";
        // 获取分页显示
        Bootstrap::unSetMaker();
        $data=$this->table->paginate([
            'list_rows'=> $this->config['_page_list_rows']?$this->config['_page_list_rows']:20,
            'query' => $this->config['_page_query']?$this->config['_page_query']:Request::param(),
            'var_page' => $this->config['_page_url_var']?$this->config['_page_url_var']:"p",
        ],$this->config['_limit']?intval($this->config['_limit']):false);

        $this->data=$data;
        if($this->config['_debug']){
            $this->debugSql[$this->tableName]=$this->table->getLastSql();
        }

//        $data = Bootstrap::make($data->items(), $data->listRows(), $data->currentPage(), $data->total(), false, [
//            'list_rows'=> $this->config['_page_list_rows']?$this->config['_page_list_rows']:20,
//            'query' => $this->config['_page_query']?$this->config['_page_query']:Request::param(),
//            'var_page' => $this->config['_page_url_var']?$this->config['_page_url_var']:"p",
//        ]);
        return $data->render(true,true,true,true,false,false);//禁用每页显示条数和跳转页码的功能
    }
    //获取数据
    public function select(){
        if(!$this->data){
            $this->data=$this->table->select();
            if($this->config['_debug']){
                $this->debugSql[$this->tableName]=$this->table->getLastSql();
            }
        }

    }
    //查询副表
    public function select_data(){
        $data=$this->data->each(function($item,$key){
            $fData=Db::name($this->tableNameData)->find($item["id"]);
            if($this->config['_debug']){
                $this->debugSql[$this->tableNameData][]=Db::name($this->tableNameData)->getLastSql();
            }            
            $item= array_merge($item,$fData);
            return $item;
        });    
        $this->data=$data;
    }
    //查询栏目主表
    public function select_cat(){
        $data=$this->data->each(function($item,$key){
            $item['cat']=Db::name("cat")->find($item['catid']);
            if($this->config['_debug']){
                $this->debugSql['cat'][]=Db::name("cat")->getLastSql();
            }               
            return $item;
        });    
        $this->data=$data;
    }
    //查询栏目副表
    public function select_cat_data(){
        $data=$this->data->each(function($item,$key){
            $fdata=Db::name("cat_data")->find($item['catid']);
            //debug
            if($this->config['_debug']){
                $this->debugSql['cat_data'][]=Db::name("cat_data")->getLastSql();
            }   
            //追加副表字段
            foreach($fdata as $k=>$v){
                $item[$k]=$v;
            }
            return $item;
        });    
        $this->data=$data;
    }
    //设置标题链接titleurl
    public function set_titleurl(){
        $data=$this->data->each(function($item,$key){
            $item['titleurl']=titleurl($item);
            return $item;
        });    
        $this->data=$data;
    }
    public function __destruct() {
        if($this->config['_debug']){
            dump([
                "where条件"=>$this->where,
                "附加条件"=>$this->config,
                "sql"=>$this->debugSql
            ]);
        }
    }
}